package org.top10.parser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.top10.collector.Collector;
import org.top10.dealor.ConfReader;
import org.top10.entity.ConfBean;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class Hoopchina extends Task {

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_Hoopchina;// 对应网站ID
	private static final String ENCoding = "utf-8";// 对应网站ID

	public Hoopchina() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new DayParser());
		PARSER.add(new WeekParser());
		PARSER.add(new MonthParser());
		PARSER.add(new ClassParser());
	}

	public static void main(String args[]) throws Exception {
		String url = "http://v.hoopchina.com/";
		ParseSource source = Collector.createCache(url, ENCoding);
		new WeekParser().parse(source);
	}
}

abstract class HoopchinaParser extends TOPParserImpl {

	public HoopchinaParser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;

	public abstract Video build(Node node, int rank) throws Exception;

	// XUNlei所有解析器的公有方法放在这里..
	protected Video buildVideo(Node dl, int rank) {
		Video v = new Video();
		v.setRank(rank);
		List<Node> as = new ArrayList<Node>();
		getAllTagsByParent(dl, "a", as);
		Node a = as.get(as.size()-1);
		String url = getAttrValueByTag(a, "href");
		v.setPlayUrl(url);
		v.setName(a.getTextContent());
		return v;
	}

	protected List hoopchinaBuild(ParseSource source, String sign) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("div","id",sign, body);
		if (node != null) {
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "li", dls);
			List<Video> list = new ArrayList<Video>();
			int rank = 1;
			for (Node d : dls) {
				Video v = build(d, rank);
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}
}

class DayParser extends HoopchinaParser {

	private static final String SIGN = "listday";
	private static final String URL="http://v.hoopchina.com/";
	private static final String TITLE="本日";
	public DayParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return hoopchinaBuild(doc,SIGN);
	}
}
class WeekParser extends HoopchinaParser {

	private static final String SIGN = "listweek";
	private static final String URL="http://v.hoopchina.com/";
	private static final String TITLE="本周";
	public WeekParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return hoopchinaBuild(doc,SIGN);
	}
}
class MonthParser extends HoopchinaParser {
	private static final String SIGN = "listmonth";
	private static final String URL="http://v.hoopchina.com/";
	private static final String TITLE="本月";
	public MonthParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return hoopchinaBuild(doc,SIGN);
	}
}
class ClassParser extends HoopchinaParser {

	private static final String SIGN = "listall";

	private static final String URL="http://v.hoopchina.com/";
	private static final String TITLE="经典";
	public ClassParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}
	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node,rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return hoopchinaBuild(doc,SIGN);
	}
}
